package com.mcxx.convert;

import cn.hutool.core.date.DateUtil;
import com.mcxx.modules.miniSalvation.order.dto.OrderInHouseDTO;
import com.mcxx.modules.temporary.family.entity.TmpFamilyMainEntity;
import com.mcxx.modules.temporary.family.entity.TmpFamilyMemberEntity;
import com.mcxx.modules.temporary.order.dto.ApplicationDTO;
import com.mcxx.modules.temporary.order.entity.TmpOrderEntity;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.IdCardUtil;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.Date;



public class TmpApplyConvert {

    public static TmpFamilyMainEntity convertToFamilyMainEntity(ApplicationDTO applyDTO, UserAccountVo accountVo) {
        TmpFamilyMainEntity familyMainEntity = new TmpFamilyMainEntity();
        BeanUtils.copyProperties(applyDTO, familyMainEntity);
        familyMainEntity.setId(applyDTO.getFamilyId());
        if(Constant.DomesticRelation.ONESELF.equals(applyDTO.getFamilyRelation())){
            familyMainEntity.setName(applyDTO.getApplyName());
            familyMainEntity.setIdcard(applyDTO.getApplyIdcard());
        }
        familyMainEntity.setMainReason(applyDTO.getMainReason());
        familyMainEntity.setFamilyObjectType(applyDTO.getObjectType());
        familyMainEntity.setTempSaveType(applyDTO.getTempSaveType());
        familyMainEntity.setOrgId(accountVo.getOrgId());
        if (StringUtils.isEmpty(applyDTO.getFamilyId())) {
            familyMainEntity.setCreateBy(accountVo.getUserId());
            familyMainEntity.setCreateTime(DateUtil.date());
            familyMainEntity.setMasterInSave(Constant.IsSaveObject.NO);
            familyMainEntity.setSaveMemberNumber(0);
        }

        return familyMainEntity;
    }

    public static TmpFamilyMemberEntity convertToFamilyMemberEntity(ApplicationDTO applyDTO, UserAccountVo accountVo) {
        TmpFamilyMemberEntity familyMemberEntity = new TmpFamilyMemberEntity();
        BeanUtils.copyProperties(applyDTO, familyMemberEntity);
        familyMemberEntity.setId(applyDTO.getMemberId());
        familyMemberEntity.setName(applyDTO.getApplyName());
        familyMemberEntity.setIdcard(applyDTO.getApplyIdcard());
        familyMemberEntity.setBirthday(DateUtil.parse(IdCardUtil.getBirthDay(applyDTO.getApplyIdcard())));
        familyMemberEntity.setSex(IdCardUtil.getSex(applyDTO.getApplyIdcard()));
        familyMemberEntity.setAge(IdCardUtil.getAge(applyDTO.getApplyIdcard()));
        familyMemberEntity.setName(applyDTO.getApplyName());
        familyMemberEntity.setIsApply(Constant.IsApply.YES);
        familyMemberEntity.setFamilyRelation(applyDTO.getFamilyRelation() );
        if (StringUtils.isEmpty(applyDTO.getMemberId())) {
            familyMemberEntity.setCreateBy(accountVo.getUserId());
            familyMemberEntity.setCreateTime(DateUtil.date());
            familyMemberEntity.setIsSaveObject(Constant.IsSaveObject.NO);
        }
        return familyMemberEntity;
    }

    public static TmpOrderEntity convertToOrderEntity(ApplicationDTO applyDTO, UserAccountVo accountVo) {
        Date dateTime = new Date();
        TmpOrderEntity orderEntity = new TmpOrderEntity();
        orderEntity.setId(applyDTO.getOrderId());
        orderEntity.setFamilyId(applyDTO.getFamilyId());
        orderEntity.setObjecttype(applyDTO.getObjectType());
        orderEntity.setOtherBusinessCode(applyDTO.getOtherBusinessCode());
        orderEntity.setTempSaveType(applyDTO.getTempSaveType());
        orderEntity.setCommitDate(applyDTO.getApplyDate());
        orderEntity.setReason(applyDTO.getReason());
        orderEntity.setApplyName(applyDTO.getApplyName());
        orderEntity.setApplyIdcard(applyDTO.getApplyIdcard());
        orderEntity.setAgentIdcard(applyDTO.getAgentIdcard());
        orderEntity.setAgentName(applyDTO.getAgentName());
        orderEntity.setAgentPhone(applyDTO.getAgentPhone());
        orderEntity.setAreaCodeCz(applyDTO.getAreaCodeCz());
        if(Constant.DomesticRelation.ONESELF.equals(applyDTO.getFamilyRelation())){
            orderEntity.setName(applyDTO.getApplyName());
            orderEntity.setIdcard(applyDTO.getApplyIdcard());
            orderEntity.setMemberId(applyDTO.getMemberId());
        }
        orderEntity.setOrgId(accountVo.getOrgId());
        orderEntity.setUpdateBy(accountVo.getUserId());
        orderEntity.setUpdateTime(dateTime);
        orderEntity.setOperator(accountVo.getUserId());
        orderEntity.setOperDate(dateTime);
        orderEntity.setDataFrom(applyDTO.getDataFrom());
        orderEntity.setField03(applyDTO.getWoaApply());
        if (StringUtils.isEmpty(orderEntity.getId())) {
            orderEntity.setCreateBy(accountVo.getUserId());
            orderEntity.setCreateTime(dateTime);
            orderEntity.setSaveMemberNumber(0);
        }

        if (StringUtils.isNotEmpty(applyDTO.getAreaCode())) {
            orderEntity.setAreaCode(applyDTO.getAreaCode());
            orderEntity.setCountyCode(applyDTO.getAreaCode().substring(0, 6));
            orderEntity.setStreetCode(applyDTO.getAreaCode().substring(0, 9));
        } else {
            orderEntity.setAreaCode(accountVo.getAreaCode());
        }
        if(CommonUtil.invalidArgs(applyDTO.getOrderId())){

            String userLevel = accountVo.getAreaLevel();
            if (Integer.parseInt(userLevel) <= 3) {

                orderEntity.setRecordFlag(Constant.OrderRecordFlag.COUNTY);
            } else {

                orderEntity.setRecordFlag(Constant.OrderRecordFlag.STREET);
            }
        }
        return orderEntity;
    }


    public static OrderInHouseDTO convertToInhouseEntity(ApplicationDTO applyDTO){
        OrderInHouseDTO inhouseDTO = new OrderInHouseDTO();
        inhouseDTO.setUpdateTime(new Date());
        inhouseDTO.setCheckPeople(applyDTO.getCheckPeople());
        inhouseDTO.setCheckDate(applyDTO.getCheckDate());
        inhouseDTO.setCheckContent(applyDTO.getCheckContent());

        inhouseDTO.setState(CommonUtil.invalidArgs(applyDTO.getInHouseState()) ? Constant.InHouseState.WAIT_CHECK : applyDTO.getInHouseState());
        inhouseDTO.setOrderId(applyDTO.getOrderId());
        inhouseDTO.setId(applyDTO.getOrderInhouseId());
        return inhouseDTO;
    }
}
